package cc.rengu.igas.channel.alipay.biz.impl;

import cc.rengu.igas.channel.alipay.common.constant.AlipayServiceConstant;
import cc.rengu.igas.channel.alipay.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.alipay.facade.AlipayService;
import cc.rengu.igas.channel.alipay.facade.request.*;
import cc.rengu.igas.channel.alipay.facade.response.*;
import cc.rengu.igas.channel.alipay.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class AlipayServiceImpl implements AlipayService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<AliCancelOrderResponse> aliCancelOrder(AliCancelOrderRequest aliCancelOrderRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliCancelOrderResponse> result = new Result<>();
        AliCancelOrderResponse cancelOrderResponse = new AliCancelOrderResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliCancelOrderRequest,
                    AlipayServiceConstant.CANCEL_ORDER_SERVICE, AliCancelOrderResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliCancelOrderResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("订单撤销失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                cancelOrderResponse.setRespDesc(((BizException) e).getCode());
                cancelOrderResponse.setRespDesc(e.getMessage());
            } else {
                cancelOrderResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                cancelOrderResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliCloseOrderResponse> aliCloseOrder(AliCloseOrderRequest aliCloseOrderRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliCloseOrderResponse> result = new Result<>();
        AliCloseOrderResponse cancelOrderResponse = new AliCloseOrderResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliCloseOrderRequest,
                    AlipayServiceConstant.CLOSE_ORDER_SERVICE, AliCloseOrderResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliCloseOrderResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("关闭订单失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                cancelOrderResponse.setRespDesc(((BizException) e).getCode());
                cancelOrderResponse.setRespDesc(e.getMessage());
            } else {
                cancelOrderResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                cancelOrderResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliCreateOrderResponse> aliCreateOrder(AliCreateOrderRequest aliCreateOrderRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliCreateOrderResponse> result = new Result<>();
        AliCreateOrderResponse createOrderResponse = new AliCreateOrderResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliCreateOrderRequest,
                    AlipayServiceConstant.CREATE_ORDER_SERVICE, AliCreateOrderResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliCreateOrderResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("创建订单失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                createOrderResponse.setRespDesc(((BizException) e).getCode());
                createOrderResponse.setRespDesc(e.getMessage());
            } else {
                createOrderResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                createOrderResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliMircopayResponse> aliMircopay(AliMircopayRequest aliMircopayRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliMircopayResponse> result = new Result<>();
        AliMircopayResponse mircopayResponse = new AliMircopayResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliMircopayRequest,
                    AlipayServiceConstant.MICROPAY_SERVICE, AliMircopayResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliMircopayResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("刷卡支付失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                mircopayResponse.setRespDesc(((BizException) e).getCode());
                mircopayResponse.setRespDesc(e.getMessage());
            } else {
                mircopayResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mircopayResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliPrecreateResponse> aliPrecreate(AliPrecreateRequest aliPrecreateRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliPrecreateResponse> result = new Result<>();
        AliPrecreateResponse precreateResponse = new AliPrecreateResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliPrecreateRequest,
                    AlipayServiceConstant.PRECREATE_SERVICE, AliPrecreateResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliPrecreateResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("交易预创建失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                precreateResponse.setRespDesc(((BizException) e).getCode());
                precreateResponse.setRespDesc(e.getMessage());
            } else {
                precreateResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                precreateResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }


    @Override
    public Result<AliQueryOrderResponse> aliQueryOrder(AliQueryOrderRequest aliQueryOrderRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliQueryOrderResponse> result = new Result<>();
        AliQueryOrderResponse queryOrderResponse = new AliQueryOrderResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliQueryOrderRequest,
                    AlipayServiceConstant.QUERY_ORDER_SERVICE, AliQueryOrderResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliQueryOrderResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("交易状态查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                queryOrderResponse.setRespDesc(((BizException) e).getCode());
                queryOrderResponse.setRespDesc(e.getMessage());
            } else {
                queryOrderResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                queryOrderResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliRefundQueryResponse> aliRefundQuery(AliRefundQueryRequest aliRefundQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliRefundQueryResponse> result = new Result<>();
        AliRefundQueryResponse refundQueryResponse = new AliRefundQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliRefundQueryRequest,
                    AlipayServiceConstant.REFUND_QUERY_SERVICE, AliRefundQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliRefundQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("交易结果通知失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                refundQueryResponse.setRespDesc(((BizException) e).getCode());
                refundQueryResponse.setRespDesc(e.getMessage());
            } else {
                refundQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                refundQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliRefundResponse> aliRefund(AliRefundRequest aliRefundRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliRefundResponse> result = new Result<>();
        AliRefundResponse refundResponse = new AliRefundResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliRefundRequest,
                    AlipayServiceConstant.REFUND_SERVICE, AliRefundResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliRefundResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("交易结果通知失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                refundResponse.setRespDesc(((BizException) e).getCode());
                refundResponse.setRespDesc(e.getMessage());
            } else {
                refundResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                refundResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliFundFreezeResponse> aliFundFreeeze(AliFundFreezeRequest aliFundFreezeRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliFundFreezeResponse> result = new Result<>();
        AliFundFreezeResponse aliFundFreezeResponse = new AliFundFreezeResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliFundFreezeRequest,
                    AlipayServiceConstant.FUND_FREEZE_SERVICE,AliFundFreezeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliFundFreezeResponse) dubboResult.getResult());
        }catch (Exception e){
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream,true));
            rglog.error("交易结果通知失败:<{}>",byteArrayOutputStream.toString());
            result.setSuccess(false);
            if(e instanceof BizException){
                aliFundFreezeResponse.setRespCode(((BizException) e).getCode());
                aliFundFreezeResponse.setRespDesc(e.getMessage());
            }else {
                aliFundFreezeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                aliFundFreezeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliFundCancelResponse> aliFundCancel(AliFundCancelRequest aliFundCancelRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliFundCancelResponse> result = new Result<>();
        AliFundCancelResponse aliFundCancelResponse = new AliFundCancelResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliFundCancelRequest,
                    AlipayServiceConstant.FUND_CANCEL_SERVICE,AliFundCancelResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliFundCancelResponse) dubboResult.getResult());
        }catch (Exception e){
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream,true));
            rglog.error("交易结果通知失败:<{}>",byteArrayOutputStream.toString());
            result.setSuccess(false);
            if(e instanceof BizException){
                aliFundCancelResponse.setRespCode(((BizException) e).getCode());
                aliFundCancelResponse.setRespDesc(e.getMessage());
            }else {
                aliFundCancelResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                aliFundCancelResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliFundUnfreezeResponse> aliFundUnfreeze(AliFundUnfreezeRequest aliFundUnfreezeRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliFundUnfreezeResponse> result = new Result<>();
        AliFundUnfreezeResponse aliFundUnfreezeResponse = new AliFundUnfreezeResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliFundUnfreezeRequest,
                    AlipayServiceConstant.FUND_UNFREEZE_SERVICE,AliFundUnfreezeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliFundUnfreezeResponse) dubboResult.getResult());
        }catch (Exception e){
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream,true));
            rglog.error("交易结果通知失败:<{}>",byteArrayOutputStream.toString());
            result.setSuccess(false);
            if(e instanceof BizException){
                aliFundUnfreezeResponse.setRespCode(((BizException) e).getCode());
                aliFundUnfreezeResponse.setRespDesc(e.getMessage());
            }else {
                aliFundUnfreezeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                aliFundUnfreezeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<AliFundOprQueryResponse> aliFundOprQuery(AliFundOprQueryRequest aliFundOprQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<AliFundOprQueryResponse> result = new Result<>();
        AliFundOprQueryResponse aliFundOprQueryResponse = new AliFundOprQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(aliFundOprQueryRequest,
                    AlipayServiceConstant.FUND_OPRQUERY_SERVICE,AliFundOprQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((AliFundOprQueryResponse) dubboResult.getResult());
        }catch (Exception e){
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream,true));
            rglog.error("交易结果通知失败:<{}>",byteArrayOutputStream.toString());
            result.setSuccess(false);
            if(e instanceof BizException){
                aliFundOprQueryResponse.setRespCode(((BizException) e).getCode());
                aliFundOprQueryResponse.setRespDesc(e.getMessage());
            }else {
                aliFundOprQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                aliFundOprQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
